<?php
/*
Template Name: Portfolio  - 3 Columns - Sidebar
*/
get_header(); ?>

<?php if ( have_posts() ) while ( have_posts() ) : the_post(); 

    $sidebar_position = get_post_meta($post->ID, 'sidebar_position', true);

    $content = get_the_content();
	$title = get_the_title();
	
	$content_class = ' class="position_left"';
	$sidebar_class = ' class="position_right"';
	
	if ( $sidebar_position == 'left' )
	{
		$content_class = ' class="position_right"';
		$sidebar_class = ' class="position_left"';
	}
	
	if ( $sidebar_position == 'website_selected' )
	{
		$website_selected_position = get_option('sidebar_position');
		
		if ( $website_selected_position == 'left' )
		{
			$content_class = ' class="position_right"';
			$sidebar_class = ' class="position_left"';
		}
	}

	// Posts per page
	$posts_per_page = get_post_meta($post->ID, 'posts_per_page', true);
	if ( $posts_per_page == '' ) $posts_per_page = '-1';

endwhile; ?>

<?php $portfolios = get_portfolios(); 

foreach ($portfolios as $portfolio):
	if (is_page($portfolio->page)): ?>
	
    	<section id="content_wrapper" class="clearfix">
			<div id="content_with_sidebar" <?php echo $content_class; ?>>
                <h1 class="page_title">
                    <?php echo $title; ?>
                </h1>
                <div class="page_content">
                    <?php the_content(); ?>
                </div><!-- .page_content -->
                                
                <?php
                
                $portfolio_ids = explode(',', $portfolio->category);
        
                $args = array(
                    'post_type' => 'portfolios',
                    'posts_per_page' => $posts_per_page,
                    'tax_query' => array(
                        array(
                            'taxonomy' => 'portfolio_category',
                            'field' => 'id',
                            'terms' => $portfolio_ids
                        )
                    ),
                    'paged' => $paged
                );
                
                $wp_query = new WP_Query( $args );
        
                global $more;
                $more = 0;
                $post_count = 0;
            

				if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post();
					
					if ( has_post_thumbnail() ) $thumbnail_src = wp_get_attachment_url( get_post_thumbnail_id( $post->ID ) );
					
					$article_margin = '';
					if ( $post_count % 3 == 2 )
						$article_margin = 'style="margin-right: 0px;"';
					
					$thumbnail_size = 'w=190&amp;h=250';
					$image_source = get_template_directory_uri().'/includes/timthumb.php?src='.$thumbnail_src.'&amp;'.$thumbnail_size.'&amp;zc=1&amp;q=100';
	
					echo '<article class="portfolio_3_columns_sidebar" '.$article_margin.'>';
					
						echo '<div class="meta clearfix">';
							echo '<div class="date">';
								 echo get_the_date('d M Y'); 
							echo '</div>';
							
							echo '<div class="posted_by">';
								echo __( 'By: ', 'reFresh' ); the_author();
							echo '</div>';
						echo '</div>';
						
						echo '<a class="portfolio_image" href="'; the_permalink(); echo '">';
							echo '<img src="'.$image_source.'" alt="'; echo the_title(); echo '" />';
						echo '</a>';
						
						echo '<div class="content">';
						
							echo '<div class="heading">';
								echo '<a href="'; the_permalink(); echo '">';
									echo '<h3>';
										echo the_title();
									echo '</h3>';
								echo '</a>';
							echo '</div>';
							
							echo '<div class="text">';
								echo the_content('');
							echo '</div>';
							
							echo '<div class="read_more">';
								echo '<a href="'; the_permalink(); echo '">';
									echo '<span class="read_more_arrow"></span>';
									echo __( 'Read more', 'reFresh' );
								echo '</a>';
							echo '</div>';
							
						echo '</div>';
						
						
					echo '</article>';
					
					if ( $post_count % 3 == 2 )
				 	   echo '<div class="content_horizontal_clear"></div>';
				    $post_count++;
			   
				endwhile; endif;
				
				if(function_exists('wp_pagenavi')) { wp_pagenavi(); }
				
            echo '</div>'; // END #content_with_sidebar
            
			// START SIDEBAR
			echo '<div id="sidebar"'.$sidebar_class.'>';
				echo '<div id="sidebar_content">';
					get_sidebar();
				echo '</div>';
			echo '</div>';
			
        ?>
            
        </section> <!-- #content_wrapper -->
    
	<?php get_footer(); 

	return;
	endif;
endforeach;
?>
